#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.sympy import *

def issimple(v):
    num, den = sympify(v).as_numer_denom()
    N = 50
    if num > N or den > N:
        return False
    else:
        return True

while True:
    # Entradas
    with display_block('Entradas'):
        coords = [One / 3, Half, Two / 3, 1, 3 * Half, 2]
        r0 = [ sympify(a) for a in
                (0,
                 oneof(-1, 1) * oneof(*coords),
                 oneof(-1, 1) * oneof(*coords)) ]
        shuffle(r0)
        x, y, z = r0

        v0 = vx, vy, vz = \
            [ sympify(a) for a in
                (oneof(-1, 1) * oneof(*coords),
                 oneof(-1, 1) * oneof(*coords),
                 oneof(-1, 1) * oneof(*coords)) ]

        r0, v0 = tuple(r0), tuple(v0)

    # Resultados
    with display_block('Respostas'):
        r2 = x * x + y * y + z * z
        correta = -(Two / (r2 ** 2)) * (x * vx + y * vy + z * vz)
        erroVelocidade = -Two / (x ** 2 + y ** 2 + z ** 2)
        erroVelocidade2 = -Two / (x ** 2 + y ** 2 + z ** 2) ** 2 * (x + y + z)
        resps = [correta, erroVelocidade, erroVelocidade2]
        if len(set(resps)) != 3 or sum(map(issimple, resps)) != 3:
            continue

        # Cria distratores
        while len(resps) < 8:
            resp = oneof(*resps) * oneof(*coords)
            if (resp not in resps) and issimple(resp):
                resps.append(resp)
        dis1, dis2, dis3, dis4, dis5 = resps[3:]
        break
